﻿using System.Collections.Generic;
using System.Linq;
using CoreLibrary;
using CoreLibrary.Paging;
using Entities.DataBase;
using Entities.DataBase.Obj;
using Entities.DataBase.Persistent;
using WebService.Service.Transfer;

namespace WebService.Service {

    public class ProductCatalogServiceX : AbstractService, IProductCatalogServiceX {

        public bool AddProductCatalog(string passport, AddProductCatalogRequest request) {
            return Run(passport, token => {
                Locate<IProductCatalogRepository>().AppendCatalog(request.PID,
                    new ProductCatalog {
                        Name = Util.GetString(request.Name),
                        Cname = Util.GetString(request.CName),
                        Description = Util.GetString(request.Description)
                    }
                );
                return true;
            });
        }

        public bool UpdateProductCatalog(string passport, UpdateProductCatalogRequest request) {
            return Run(passport, token => {
                Locate<IProductCatalogRepository>().UpdateCatalog(
                    request.ID,
                    Util.GetString(request.Name),
                    Util.GetString(request.CName),
                    Util.GetString(request.Description)
                );
                return true;
            });
        }

        public bool RemoveProductCatalog(string passport, int catId) {
            return Run(passport, token => {
                Locate<IProductCatalogRepository>().DeleteCatalog(catId);
                return true;
            });
        }

        public ProductCatalogInfo GetProductCatalog(string passport, int catalogId) {
            return Run(passport, token => {
                var catalog = Services.Locate<IProductCatalogRepository>()
                                      .GetCatalog(catalogId);
                return Convert(catalog);
            });
        }

        public List<ProductCatalogInfo> GetLeafProductCatalogs(string passport) {
            return Run(passport, token => Services.Locate<IProductCatalogRepository>()
                                                  .GetLeafCatalogs()
                                                  .Select(Convert)
                                                  .ToList());
        }

        public List<ProductCatalogInfo> GetAllProductCatalogs(string passport) {
            return Run(passport, token => {
                var catalogs = Services.Locate<IProductCatalogRepository>()
                                       .GetAll();
                return catalogs.Select(Convert)
                               .ToList();
            });
        }

        public List<ProductCatalogInfo> GetProductCatalogs(string passport, int catId) {
            return Run(passport, token => {
                var catalogs = Services.Locate<IProductCatalogRepository>().GetChildren(catId);
                return catalogs.Select(Convert)
                               .ToList();
            });
        }

        public PagingResult<ProductCatalogInfo> ListProductCatalogs(string passport, int catId, PagingRequest request) {
            return Run(passport, token => {
                var catalogs = Services.Locate<IProductCatalogRepository>().GetChildren(catId);
                return new PagingResult<ProductCatalogInfo> {
                    Offset = request.Offset,
                    Data = catalogs.Skip(request.Offset)
                                   .Take(request.Limit)
                                   .Select(Convert)
                                   .ToList()
                };
            });
        }

        private static ProductCatalogInfo Convert(ProductCatalog catalog) {
            return new ProductCatalogInfo {
                Id = catalog.Id,
                PID = catalog.PID,
                LID = catalog.LID,
                RID = catalog.RID,
                Order = catalog.ListOrder,
                Level = catalog.Level,
                Name = catalog.Name,
                CName = catalog.Cname,
                IsLeaf = catalog.IsLeaf,
                IsRoot = catalog.IsRoot,
                Description = catalog.Description

            };
        }
       
    }
}
